package com.tsp_tux.demo.Util;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @author 刘海生
 */
public class Login {

    private static final String LOGIN_ADDRESS = "https://pass.sdu.edu.cn/cas/login?service=http%3A%2F%2Fbkjws.sdu.edu.cn%2Ff%2Fj_spring_security_thauth_roaming_entry";
    private static final String STUDENT_DETAIL_ADDRESS = "http://bkjws.sdu.edu.cn/b/grxx/xs/xjxx/detail";
    private static final String TEACHER_DETAIL_ADDRESS = "http://bkjws.sdu.edu.cn/b/jxrl/js/jslxfsb/detail/";
    private static final String TREE_ADDRESS = "http://bkjws.sdu.edu.cn/b/common/main/tree";

    /**
     * 学生登录，并返回个人信息
     *
     * @param rsa 前端用学号、密码、lt计算好的rsa
     * @param ul  学号长度
     * @param pl  密码长度
     * @return 如果登录成功，返回用户信息 JSON，否则返回 null
     * @throws IOException 如果发生网络错误或其它错误
     */
    public static String studentLogin(String rsa, int ul, int pl) throws IOException {
        Lt lt = new Lt(LOGIN_ADDRESS);

        lt.obtain();

        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(LOGIN_ADDRESS).openConnection();
        httpURLConnection.setInstanceFollowRedirects(false);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("Cookie", lt.getCookies());
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.connect();

        BufferedOutputStream outputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
        outputStream.write(String.format("rsa=%s&ul=%d&pl=%d&lt=%s&execution=e1s1&_eventId=submit", rsa, ul, pl, lt.getLt()).getBytes(StandardCharsets.UTF_8));
        outputStream.flush();

        int code = httpURLConnection.getResponseCode();
        if (code != 302) return null;

        String location = httpURLConnection.getHeaderField("Location");
        if (location.startsWith("/")) {
            location = "https://pass.sdu.edu.cn" + location;
        }

        F302 f302 = new F302();
        f302.request(location);

        return f302.request(STUDENT_DETAIL_ADDRESS);
    }

    public static String teacherLogin(String user, String rsa, int ul, int pl) throws IOException {
        Lt lt = new Lt(LOGIN_ADDRESS);

        lt.obtain();

        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(LOGIN_ADDRESS).openConnection();
        httpURLConnection.setInstanceFollowRedirects(false);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("Cookie", lt.getCookies());
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.connect();
        BufferedOutputStream outputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
        outputStream.write(String.format("rsa=%s&ul=%d&pl=%d&lt=%s&execution=e1s1&_eventId=submit", rsa, ul, pl, lt.getLt()).getBytes(StandardCharsets.UTF_8));
        outputStream.flush();

        int code = httpURLConnection.getResponseCode();
        if (code != 302) return null;

        String location = httpURLConnection.getHeaderField("Location");
        if (location.startsWith("/")) {
            location = "https://pass.sdu.edu.cn" + location;
        }

        F302 f302 = new F302();
        f302.request(location);

        return f302.request(TEACHER_DETAIL_ADDRESS + user);
    }

    public static String login(Lt lt, String uid, String rsa, int ul, int pl) throws IOException {

        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(LOGIN_ADDRESS).openConnection();
        httpURLConnection.setInstanceFollowRedirects(false);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("Cookie", lt.getCookies());
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.connect();

        BufferedOutputStream outputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
        outputStream.write(String.format("rsa=%s&ul=%d&pl=%d&lt=%s&execution=e1s1&_eventId=submit", rsa, ul, pl, lt.getLt()).getBytes(StandardCharsets.UTF_8));
        outputStream.flush();

        int code = httpURLConnection.getResponseCode();
        if (code != 302) return null;

        String location = httpURLConnection.getHeaderField("Location");
        if (location.startsWith("/")) {
            location = "https://pass.sdu.edu.cn" + location;
        }

        F302 f302 = new F302();
        f302.request(location);

        //获得菜单树，根据菜单树来判断是什么身份
        String treeContent = f302.request(TREE_ADDRESS);
        if (treeContent.contains("教师成绩录入")){
            return f302.request(TEACHER_DETAIL_ADDRESS + uid);
        }else {
            return f302.request(STUDENT_DETAIL_ADDRESS);
        }

    }
}
